﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.SqlClient;

public partial class Admin_DevMan_Revise : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //详细信息
            string proid = Session["Admin_Detail_ProID"].ToString();
            //Response.Write(proid);
            string pronum = "";
            string proname = "";
            string danwei = "";
            string place = "";
            string auditor = "";
            string condition = "";
            string category = "";
            string buydate = "";
            string ordernot = "";
            string manuname = "";
            string manuinfo = "";
            string worth = "";
            string operation = "";
            string function = "";
            string remarks = "";
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
            try
            {
                conn.Open();
                SqlCommand cmdmeg = new SqlCommand();
                string SQL = "select pro_num,pro_name,pro_danwei,pro_location,auditor,condition,pro_category,buydate,ordernot,pro_manuname,pro_manuinfo,pro_worth,operation_manual,pro_function,remarks from Products where (pro_num=@pronum and flag=1)";
                cmdmeg.Connection = conn;
                cmdmeg.CommandText = SQL;
                cmdmeg.CommandType = CommandType.Text;
                //提交参数 SQL 语句中必须要有@参数
                SqlParameter pnum = new SqlParameter("@pronum", SqlDbType.NVarChar, 10);
                pnum.Value = proid;
                cmdmeg.Parameters.Add(pnum);
                //如果还有其他参数，一并添加
                SqlDataReader reader = cmdmeg.ExecuteReader();
                //使用 SqlDataReader循环读取数据

                while (reader.Read())      //如果可以读（有数据），就开始读
                {
                    pronum = reader["pro_num"].ToString();
                    proname = reader["pro_name"].ToString();
                    danwei = reader["pro_danwei"].ToString();
                    place = reader["pro_location"].ToString();
                    auditor = reader["auditor"].ToString();
                    condition = reader["condition"].ToString();
                    category = reader["pro_category"].ToString();
                    buydate = reader["buydate"].ToString();
                    ordernot = reader["ordernot"].ToString();
                    manuname = reader["pro_manuname"].ToString();
                    manuinfo = reader["pro_manuinfo"].ToString();
                    worth = reader["pro_worth"].ToString();
                    operation = reader["operation_manual"].ToString();
                    function = reader["pro_function"].ToString();
                    remarks = reader["remarks"].ToString();
                }
                FProNum.Text = pronum;
                FProName.Text = proname;
                FDanWei.Text = danwei;
                FProPlace.Text = place;
                FDutyName.Text = auditor;
                RBLZT.SelectedValue = condition;

                FProCateg.SelectedValue = category;
                FDate.Text = buydate;
                RBLYY.SelectedValue = ordernot;
                FManuName.Text = manuname;
                //HyperLink1.NavigateUrl = "Operation/" + operation;
                FProRemarks.Text = remarks;
                FProWorth.Text = worth;

                FManuInfo.Text = manuinfo;
                FProFunction.Text = function;
                //HyperLink2.Text = manuinfo;
                //HyperLink2.NavigateUrl = manuinfo;
                //Label312.Text = function;
                //Label313.Text = remarks;

            }
            finally
            {
                conn.Close();
            }
        }
        

}


    protected void ButRevise_Click(object sender, EventArgs e)
    {
        string pro_num = Session["Admin_Detail_ProID"].ToString();
        //string pronum = FProNum.Text.Trim();
        string proname = FProName.Text.Trim();
        string danwei = FDanWei.Text.Trim();
        string place = FProPlace.Text.Trim();
        string auditor = FDutyName.Text.Trim();
        string condition = RBLZT.SelectedValue;
        string category = FProCateg.SelectedValue;
        //string category = "教学设备";
        string buydate = FDate.Text.Trim();
        string ordernot = RBLYY.SelectedValue;
        string manuname = FManuName.Text.Trim();  
        string worth = FProWorth.Text.Trim();
        string remarks = FProRemarks.Text.Trim();

        string function = FProFunction.Text.Trim();
        string manuinfo = FManuInfo.Text.Trim();
        string operation = "";
        string proimg = "";

        string IFileName = pro_num;
        string IFileFoldName = "/Photos/";
        string IFilePath = IFileFoldName + IFileName;
        bool IfileIsValid = false;
        //如果确认了文件上传，则判断文件类型是否符合要求
        if (this.FileUploadImg.HasFile)
        {
            //获取上传文件的后缀名
            String fileExtension = System.IO.Path.GetExtension(this.FileUploadImg.FileName).ToLower();//ToLower是将Unicode字符的值转换成它的小写等效项
                                                                                                      //定义一个数组，把文件后缀名的的类型总结出来
            String[] restrictExtension = { ".jpg", ".bmp", ".png", "jpeg" };
            //判断文件类型是否符合要求
            for (int i = 0; i < restrictExtension.Length; i++)
            {
                if (fileExtension == restrictExtension[i])
                {
                    IfileIsValid = true;
                    IFileName += restrictExtension[i];
                }
            }
        }
        IFilePath = IFileFoldName + IFileName;
        string IFileFullPath = Server.MapPath(IFilePath);
        //如果文件类型符合要求，则用SaveAs方法实现上传，并显示信息
        if (IfileIsValid == true)
        {
            try
            {
                FileUploadImg.SaveAs(IFileFullPath);
                proimg = IFileName;

            }
            catch
            {
                FileUploadImg_Label.Text = "图片格式不正确，请重新上传!";
            }
        }
        else
        {
            Response.Write("<script>alert('图片格式不正确，请重新上传！')</script>");
            //FileUploadImg_Label.Text = "图片格式不正确，请重新上传!";
            //Response.End();
        }

        //操作手册
        string OFileName = pro_num;
        string OFileFoldName = "/Operation/";
        string OFilePath = OFileFoldName + OFileName;
        bool OfileIsValid = false;
        if (this.FileUploadUse.HasFile)
        {
            String fileExtension = System.IO.Path.GetExtension(this.FileUploadUse.FileName).ToLower();
            String[] restrictExtension = { ".pdf", ".doc", ".docx", ".jpg", ".png" };
            for (int i = 0; i < restrictExtension.Length; i++)
            {
                if (fileExtension == restrictExtension[i])
                {
                    OfileIsValid = true;
                    OFileName += restrictExtension[i];
                }
            }
        }
        
        OFilePath = OFileFoldName + OFileName;
        string OFileFullPath = Server.MapPath(OFilePath);
        if (OfileIsValid == true)
        {
            try
            {
                FileUploadUse.SaveAs(OFileFullPath);
                operation = OFileName;

            }
            catch
            {
                FileUploadUse_Label.Text = "不支持操作手册文件格式，请重新上传！";
            }
        }
        else
        {
            Response.Write("<script>alert('不支持操作手册文件格式，请重新上传！')</script>"); 
            //FileUploadUse_Label.Text = "不支持操作手册文件格式，请重新上传！";
            //Response.End();
        }

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            string SQL = "update Products set pro_name=@pro_name,pro_danwei=@pro_danwei,pro_location=@pro_location,auditor=@auditor,condition=@condition,pro_category=@pro_category,buydate=@buydate,ordernot=@ordernot,pro_manuname=@pro_manuname,pro_manuinfo=@pro_manuinfo,pro_worth=@pro_worth,pro_function=@pro_function,remarks=@remarks where (pro_num=@pro_num and flag=1)";
            cmd.Connection = conn;
            cmd.CommandText = SQL;
            cmd.CommandType = CommandType.Text;
            //提交参数 SQL 语句中必须要有@参数
            SqlParameter pnum = new SqlParameter("@pro_num", SqlDbType.NVarChar, 10);
            pnum.Value = pro_num;
            cmd.Parameters.Add(pnum);
           
            SqlParameter ppro_name = new SqlParameter("@pro_name", SqlDbType.NVarChar, 20);
            ppro_name.Value = proname;
            cmd.Parameters.Add(ppro_name);

            //SqlParameter ppro_img = new SqlParameter("@pro_img", SqlDbType.NVarChar, 30);
            //ppro_img.Value = proimg;
            //cmd.Parameters.Add(ppro_img);

            SqlParameter ppro_danwei = new SqlParameter("@pro_danwei", SqlDbType.NVarChar, 20);
            ppro_danwei.Value = danwei;
            cmd.Parameters.Add(ppro_danwei);

            SqlParameter ppro_manuname = new SqlParameter("@pro_manuname", SqlDbType.NVarChar, 20);
            ppro_manuname.Value = manuname;
            cmd.Parameters.Add(ppro_manuname);

            SqlParameter pmanuinfo = new SqlParameter("@pro_manuinfo", SqlDbType.NVarChar, 40);
            pmanuinfo.Value = manuinfo;
            cmd.Parameters.Add(pmanuinfo);

            SqlParameter pbuydate = new SqlParameter("@buydate", SqlDbType.NVarChar, 20);
            pbuydate.Value = buydate;
            cmd.Parameters.Add(pbuydate);

            SqlParameter ppro_location = new SqlParameter("@pro_location", SqlDbType.NVarChar, 20);
            ppro_location.Value = place;
            cmd.Parameters.Add(ppro_location);

            SqlParameter pauditor = new SqlParameter("@auditor", SqlDbType.NVarChar, 10);
            pauditor.Value = auditor;
            cmd.Parameters.Add(pauditor);

            SqlParameter pcondition = new SqlParameter("@condition", SqlDbType.NVarChar, 10);
            pcondition.Value = condition;
            cmd.Parameters.Add(pcondition);

            SqlParameter pordernot = new SqlParameter("@ordernot", SqlDbType.NVarChar, 10);
            pordernot.Value = ordernot;
            cmd.Parameters.Add(pordernot);

            SqlParameter ppro_worth = new SqlParameter("@pro_worth", SqlDbType.NVarChar, 10);
            ppro_worth.Value = worth;
            cmd.Parameters.Add(ppro_worth);

            SqlParameter ppro_category = new SqlParameter("@pro_category", SqlDbType.NVarChar, 20);
            ppro_category.Value = category;
            cmd.Parameters.Add(ppro_category);

            SqlParameter ppro_function = new SqlParameter("@pro_function", SqlDbType.NVarChar, 100);
            ppro_function.Value = function;
            cmd.Parameters.Add(ppro_function);

            //SqlParameter poper = new SqlParameter("@operation_manual", SqlDbType.NVarChar, 20);
            //poper.Value = operation;
            //cmd.Parameters.Add(poper);

            SqlParameter premarks = new SqlParameter("@remarks", SqlDbType.NVarChar, 100);
            premarks.Value = remarks;
            cmd.Parameters.Add(premarks);

            int R = cmd.ExecuteNonQuery();  //R 的值就是返回影响表的行数
            if (R > 0)
            {
                Session["ReturnPath"] = "Admin_DevMan_Revise.aspx"/*id="+pro_num*/;
                Session["Message"] = "恭喜，操作成功！";
                Response.Redirect("Success.aspx");
            }
            else
            {
                Session["Message"] = "修改失败，请检查您的操作！";
                Response.Redirect("Error.aspx");
            }


        }
        finally
        {
            conn.Close();
        }
    }

  
}